/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package petstore.view.action.admin;

import petstore.view.action.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.Globals;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.validator.DynaValidatorActionForm;
import petstore.business.IAdminFacade;
import petstore.model.Usuario;

/**
 *
 * @author Tom Hayden
 */
public class AlterarSenhaAction extends CustomDispatchAction {

    private IAdminFacade adminFacade;

    public void setAdminFacade(IAdminFacade adminFacade) {
        this.adminFacade = adminFacade;
    }

    public ActionForward salvar(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        String forward = SUCCESS;
        this.setDefaultReturnPath(request);

        DynaValidatorActionForm f = (DynaValidatorActionForm) form;
        ActionErrors errors = new ActionErrors();
        Usuario usuario = (Usuario) request.getSession().getAttribute("user");
        boolean alterouSenha = this.adminFacade.alterarSenha(usuario, f.getString("senha"));
        if (!alterouSenha) {
            errors.add("error", new ActionMessage("alterarsenha.error.falhanaalteracao"));
            forward = "mesmaPagina";
        }
        
        if (!errors.isEmpty()) {
            request.setAttribute(Globals.ERROR_KEY, errors);
        }

        return mapping.findForward(forward);
    }
}
